using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Activa.CoolStorage;
using CodeStory.Core;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.Services;
using CodeStory.Core.UI;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_ConfigPortlets")]
	public partial class ConfigPortlets : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(ConfigPortlets_Load);
			btnInstall.Click += new EventHandler(ButtonInstall_Click);
			base.OnInit(e);
		}

		void ButtonInstall_Click(object sender, EventArgs e) {
			string moduleid = Request.QueryString["moduleid"];
			Guid id = new Guid(moduleid);
			foreach (GridViewRow row in notinstalledGridView.Rows) {
				CheckBox chk = (CheckBox)row.FindControl("chkAdd");
				if (!chk.Checked) continue;
				TextBox txtName = (TextBox)row.FindControl("txtPortletName");
				TextBox txtDesc = (TextBox)row.FindControl("txtDescription");
				TextBox txtTimesLimited = (TextBox)row.FindControl("txtTabTimesLimited");
				Label lblPath = (Label)row.FindControl("lblPortletPath");
				PortletObject obj = PortletObject.New();
				obj.PortletID = Guid.NewGuid();
				obj.ModuleID = id;
				obj.PortletName = txtName.Text;
				obj.PortletDescription = txtDesc.Text;
				obj.PortletPath = lblPath.Text;
				obj.TimesLimited = short.Parse(txtTimesLimited.Text);
				obj.Save();
			}
			BindInstalledPortlets();
		}

		void ConfigPortlets_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (EditMode) return;
			string moduleid = Request.QueryString["moduleid"];
			if (string.IsNullOrEmpty(moduleid) && !EditMode) {
				throw new NeedQueryStringException("moduleid");
			}
			if (!IsPostBack) {
				BindInstalledPortlets();
			}
		}

		private void BindInstalledPortlets() {
			string moduleid = Request.QueryString["moduleid"];
			Guid id = new Guid(moduleid);
			ModuleObject module = ModuleObject.Read(id);
			CSList<PortletObject> list = module.Portlets;
			installedGridView.DataSource = list;
			installedGridView.DataBind();

			BindNotInstalledPortlets(module);
		}

		private void BindNotInstalledPortlets(ModuleObject module) {
			string mdir = FileUtilityService.Combine(Request.PhysicalApplicationPath,"Skins\\default");
			string pdir = FileUtilityService.Combine(mdir,module.InstallDirectory);
			DirectoryInfo di = new DirectoryInfo(pdir);
			List<PortletItem> list = GetAllPortlets(di, new List<PortletItem>(), pdir,module.InstallDirectory);
			foreach (PortletObject obj in module.Portlets) {
				string fullname = FileUtilityService.Combine(mdir, obj.PortletPath);
				PortletItem item = FindItemInList(list, fullname);
				if (item != null) {
					list.Remove(item);
				}
			}
			notinstalledGridView.DataSource = list;
			notinstalledGridView.DataBind();
		}

		private PortletItem FindItemInList(List<PortletItem> list, string fullname) {
			foreach (PortletItem item in list) {
				if (item.FullName.Equals(fullname, StringComparison.OrdinalIgnoreCase)) {
					return item;
				}
			}
			return null;
		}

		private List<PortletItem> GetAllPortlets(DirectoryInfo di, List<PortletItem> list, string pdir, string moduleInstallDirectory) {
			foreach (FileInfo fi in di.GetFiles("*.ascx")) {
				PortletItem item = new PortletItem(fi.FullName,pdir,moduleInstallDirectory);
				list.Add(item);
			}
			foreach (DirectoryInfo d in di.GetDirectories()) {
				GetAllPortlets(d,list,pdir,moduleInstallDirectory);
			}
			return list;
		}

		private class PortletItem{
			private string fullName = string.Empty;
			private string portletPath = string.Empty;
			string prePath = string.Empty;
			private string moduleInstallDirecotry = string.Empty;

			public PortletItem(string fullName, string prePath,string moduleInstallDirecotry) {
				this.FullName = fullName;
				this.prePath = prePath;
				this.moduleInstallDirecotry = moduleInstallDirecotry;
			}

			public string FullName {
				get { return fullName; }
				set { 
					fullName = value.Replace("\\","/");
				}
			}
			public string PortletPath {
				get {
					if (string.IsNullOrEmpty(portletPath)) {
						portletPath = moduleInstallDirecotry + FullName.Substring(prePath.Length);
						portletPath = portletPath.Trim('/');
					}
					return portletPath; 
				}
			}
		}
	}
}